package cn.stylefeng.guns.modular.budget.controller;

import cn.stylefeng.guns.core.common.constant.factory.PageFactory;
import cn.stylefeng.guns.core.common.page.PageInfoBT;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.modular.account.service.IAccountService;
import cn.stylefeng.guns.modular.classification.service.IClassificationService;
import cn.stylefeng.guns.modular.family.service.IFamilyService;
import cn.stylefeng.guns.modular.familynum.service.IFamilynumService;
import cn.stylefeng.guns.modular.system.model.*;
import cn.stylefeng.guns.modular.system.service.IUserService;
import cn.stylefeng.guns.modular.system.warpper.BudgetWarpper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import cn.stylefeng.guns.modular.budget.service.IBudgetService;

import java.math.BigDecimal;
import java.util.*;

/**
 * 预算表控制器
 *
 * @author fengshuonan
 * @Date 2021-04-12 14:34:30
 */
@Controller
@RequestMapping("/budget")
public class BudgetController extends BaseController {

    private String PREFIX = "/budget/budget/";

    @Autowired
    private IBudgetService budgetService;

    @Autowired
    private IAccountService accountService;

    @Autowired
    private IClassificationService classificationService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IFamilyService familyService;

    @Autowired
    private IFamilynumService familynumService;


    /**
     * 跳转到预算表首页
     */
    @RequestMapping("")
    public String index(Model model) {

        Integer userid=ShiroKit.getUser().getId();
        EntityWrapper<Budget> budgetEntityWrapper=new EntityWrapper<>();
        budgetEntityWrapper.eq("updateid",userid);
        List<Budget> budgetList=budgetService.selectList(budgetEntityWrapper);
        BigDecimal bigDecima=BigDecimal.valueOf(0);
        for(int i=0;i<budgetList.size();i++){
            bigDecima=bigDecima.add(budgetList.get(i).getBudgetmoney());
        }
        model.addAttribute("bigDecima",bigDecima);
        model.addAttribute("num",budgetList.size());


        EntityWrapper<Classification> classificationEntityWrapper=new EntityWrapper<>();
        classificationEntityWrapper.eq("pcode","yslx");
        List<Classification> classificationList=classificationService.selectList(classificationEntityWrapper);
        model.addAttribute("classificationList",classificationList);



        return PREFIX + "budget.html";
    }

    /**
     * 跳转到添加预算表
     */
    @RequestMapping("/budget_add")
    public String budgetAdd(Model model) {


        EntityWrapper<Classification> classificationEntityWrapper=new EntityWrapper<>();
        classificationEntityWrapper.eq("pcode","yslx");
        List<Classification> classifications=classificationService.selectList(classificationEntityWrapper);
        model.addAttribute("classifications",classifications);


        return PREFIX + "budget_add.html";
    }

    /**
     * 跳转到修改预算表
     */
    @RequestMapping("/budget_update/{budgetId}")
    public String budgetUpdate(@PathVariable Integer budgetId, Model model) {

//

        EntityWrapper<Classification> classificationEntityWrapper=new EntityWrapper<>();
        classificationEntityWrapper.eq("pcode","yslx"); //支出类型
        List<Classification> classifications=classificationService.selectList(classificationEntityWrapper);
        model.addAttribute("classifications",classifications);

        Budget budget = budgetService.selectById(budgetId);
        model.addAttribute("item",budget);
        LogObjectHolder.me().set(budget);
        return PREFIX + "budget_edit.html";
    }

    /**
     * 获取预算表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition1,String condition3,Model model) {
//        return budgetService.selectList(null);

        Page<Map<String,Object>> page=new PageFactory<Map<String,Object>>().defaultPage();


        Integer userid=ShiroKit.getUser().getId(); //用户id


        EntityWrapper<Budget> budgetEntityWrapper=new EntityWrapper<>();

//        System.out.println(condition1);
        if(condition1==null){
            System.out.println("*****"+null+"******");
            Calendar cal = Calendar.getInstance();
            Integer year = cal.get(Calendar.YEAR);//获取年
            Integer month = cal.get(Calendar.MONTH )+1;//获取月

            String x=year.toString()+"-";
            if(month<10){
                x=x+"0";
            }
            x=x+month.toString();
            System.out.println(x);
            budgetEntityWrapper.like("createtime",x);
        }
        else{
            System.out.println("*****"+condition1+"******");
            budgetEntityWrapper.like("createtime",condition1);
        }

        budgetEntityWrapper.like("budgetkind",condition3);

        budgetEntityWrapper.orderBy("id",false);
        budgetEntityWrapper.eq("updateid",userid);


        Page<Map<String,Object>> mapPage=budgetService.selectMapsPage(page,budgetEntityWrapper);
        page=new BudgetWarpper(mapPage).wrap();
        return new PageInfoBT<>(page);
    }

    /**
     * 新增预算表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Budget budget) {
        budget.setCreatetime(new Date());
        System.out.println("******"+ShiroKit.getUser().getId());
        budget.setUpdateid(ShiroKit.getUser().getId());

        //预算类型
        String thisKind=budget.getBudgetkind();

        Calendar cal = Calendar.getInstance();
        Integer year = cal.get(Calendar.YEAR);//获取年
        Integer month = cal.get(Calendar.MONTH )+1;//获取月
        String x=year.toString()+"-";
        if(month<10){
            x=x+"0";
        }
        x=x+month.toString();


        EntityWrapper<Budget> budgetEntityWrapper=new EntityWrapper<>();
        budgetEntityWrapper.like("createtime",x);
        budgetEntityWrapper.eq("updateid",ShiroKit.getUser().getId());

        List<Budget>budgetList=budgetService.selectList(budgetEntityWrapper);
        boolean flag=true;
        for(int i=0;i<budgetList.size();i++){
            Budget budget1=budgetList.get(i);
            if((budget1.getBudgetkind().equals(thisKind))){
                budget1.setBudgetmoney(budget1.getBudgetmoney().add(budget.getBudgetmoney()));
                flag=false;

                budgetService.updateById(budget1);
            }
        }


        if(flag){
            budgetService.insert(budget);
        }

        return SUCCESS_TIP;
    }

    /**
     * 删除预算表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer budgetId) {
        budgetService.deleteById(budgetId);
        return SUCCESS_TIP;
    }

    /**
     * 修改预算表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Budget budget) {
        budget.setUpdatetime(new Date());
        Integer userid=ShiroKit.getUser().getId();
        budget.setUpdateid(userid);

        budgetService.updateById(budget);
        return SUCCESS_TIP;
    }

    /**
     * 预算表详情
     */
    @RequestMapping(value = "/detail/{budgetId}")
    @ResponseBody
    public Object detail(@PathVariable("budgetId") Integer budgetId) {
        return budgetService.selectById(budgetId);
    }
}
